% plp_header = ['1.year|2.month|3.day|4.hh|5.mm|6.ss|7.localtime|8.longitude|9.radius|10.kp|11.smooth|12.uncertainty|...
%     13.lon|14.lat|15.magn.lat|16.radius|17.lvalue|18.electron density(cm-3)|19.curvature|20.CTR|21.hemisphere(<-north)|...
%     22.lon|23.lat|24.magn.lat|25.radius|26.lvalue|27.electron density(cm-3)|28.curvature|29.CTR|30.hemisphere(<-south)|...
%     31.lon|32.lat|33.magn.lat|34.radius|35.lvalue|36.electron density(cm-3)|37.curvature|38.CTR|39.hemisphere(<-trough)'];

PLP = zeros([1544,39])* NaN;
fid = fopen('c:\manoj\projects\plasma\IRIPLP_EOP_JT_09-17LT_268-298LON_new.txt','rt');
%fid = fopen('c:\manoj\projects\plasma\IRIPLP_EOP_JT_09-17LT_268-298LON_400_lv_400_edens.txt','rt');
tag = 0;

S = ' ';
counter = 0;
while S ~= -1,
    S = fgetl(fid);
    if S(1) == '2',
        counter = counter +1;
        PLP(counter,1:12) = sscanf(S,'%f');
    elseif S(1) == ' ' & S(end-5:end) == ' north',
        PLP(counter,13:20) = sscanf(S,'%f');
        switch S(end-5:end),
            case ' north',
            PLP(counter,21) = 1;
            case ' south',
            PLP(counter,21) = 2;
            case 'trough',
            PLP(counter,21) = 3;
        end;
    elseif S(1) == ' ' & S(end-5:end) == ' south',
        PLP(counter,22:29) = sscanf(S,'%f');
        switch S(end-5:end),
            case ' north',
            PLP(counter,30) = 1;
            case ' south',
            PLP(counter,30) = 2;
            case 'trough',
            PLP(counter,30) = 3;
        end;
    elseif S(1) == ' ' & S(end-5:end) == 'trough',
        PLP(counter,31:38) = sscanf(S,'%f');
        switch S(end-5:end),
            case ' north',
            PLP(counter,39) = 1;
            case ' south',
            PLP(counter,39) = 2;
            case 'trough',
            PLP(counter,39) = 3;
        end;   
    end;
end;     

%save c:\manoj\projects\plasma\julia_PLP fday w sigma t PLP; 
more_n = [48,88,101,111,112,252,260,261,268,270,277,339,340,376,378,491,493,502,509,530,556,564,566,570,...
    603,604,641,652,658,672,698,746,547];
less_n = [161,278,280,282,342,361,366,453,517,536,673,705,717,738];
still_less_n = [62,69,347,353,356,548,704,706,578,590];
ploy_n = 12;
xaxis = 1:132;
i=1;
for i = 1:749,
        fflag = 0;
        L = isnan(w(i,:)) | w(i,:) > 55 | w(i,:) < -25;
        k = find(L==0);
        if sum(ismember(more_n,i)) == 1,
            poly_n = 16;
        elseif sum(ismember(less_n,i)) == 1,
            poly_n = 6;
        elseif sum(ismember(still_less_n,i)) == 1,
            poly_n = 4;            
        else,
            poly_n = 12;
        end;
        [p,s] = polyfit(xaxis(~L),w(i,~L),poly_n);
        y = polyval(p,xaxis(k(1):k(end)));
        sstd = std(y)/length(y);
        Julia_W(i).p = p;
        Julia_W(i).fday = floor(fday(i,1));
        Julia_W(i).k = k;
        if sstd > 0.1,
            [p] = polyfit(xaxis(~L),w(i,~L),9);
            Julia_W(i).p = p;
            fflag = 1;
        end;
    end;
    
    xxaxis = ((fday(1,:)-floor(fday(1,:)))*24)-5;    
    i=1;
    for kp = 1:75,
        for kk = 1:10,
            subplot(5,2,kk);
        hold off;
       % plot(xaxis,w(i,:),'b*');
       plot(xxaxis,w(i,:),'b*');
        k = Julia_W(i).k;
        p = Julia_W(i).p;
        fday1 = Julia_W(i).fday;
        y1 = polyval(p,xaxis(k(1)):0.1:xaxis(k(end)));
        hold on;
%         plot(xaxis(k(1):k(end)),y,'r','LineWidth',2);
%        plot(xaxis(k(1)):0.1:xaxis(k(end)),y1,'r','LineWidth',2);
        plot(xxaxis(k(1)):(5*0.099999999)/(60):xxaxis(k(end)),y1,'r','LineWidth',2);
        grid on;
        title(sprintf('Number - %d fday - %d',i,floor(fday1)));
        i=i+1;
        end;
        pause;
    end;
 

